package com.ps.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ps.pojo.LineInfo;
import com.ps.pojo.PoleInfo;
import com.ps.mapper.PoleInfoMapper;
import com.ps.service.LineInfoService;
import com.ps.service.PoleInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 杆塔信息表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-12-18
 */
@Service("poleInfoService")
public class PoleInfoServiceImpl extends ServiceImpl<PoleInfoMapper, PoleInfo> implements PoleInfoService {
    @Resource
    private PoleInfoMapper poleInfoMapper;

    @Resource
    private LineInfoService lineInfoService;
    @Override
    public Page<PoleInfo> getAll(Integer page, Integer limit, PoleInfo poleInfo) {
        QueryWrapper<PoleInfo> wrapper=new QueryWrapper<>();
        Page<PoleInfo>infoPage =new Page<>(page,limit);
        wrapper.eq(poleInfo.getIsstart()>=0,
                "isstart",poleInfo.getIsstart());
        wrapper.eq(poleInfo.getLineid()>=0,
                "lineid",poleInfo.getLineid());
        wrapper.like(poleInfo.getPolecode()!=null&&
                        !poleInfo.getPolecode().equals("")
                ,"polecode"
                ,poleInfo.getPolecode());
        poleInfoMapper.selectPage(infoPage,wrapper);
        return infoPage;
    }

    @Override
    public void add(String qian, Integer num) {
        PoleInfo poleInfo=poleInfoMapper.getPole(qian);
        System.out.println(poleInfo);
        String poleCode=null;
        int x=0;
        if (poleInfo!=null){
             poleCode=poleInfo.getPolecode().substring(qian.length());
             x=Integer.parseInt(poleCode);
        }
        PoleInfo pole=new PoleInfo();
        pole.setCreatedby("创建人");
        pole.setCreationtime(LocalDateTime.now());
        pole.setIsstart(1);
        for (int i=x+1;i<=x+num;i++){
            Integer c= String.valueOf(i).length();
            String m="";
            for (int k=c;k<4;k++){
                m+="0";
            }
            pole.setPolecode(qian+m+i);
            poleInfoMapper.insert(pole);
        }
    }

    @Override
    public Boolean update(Integer id, Integer isstart, Integer lineid) {
        LineInfo byId = lineInfoService.getById(lineid);
        PoleInfo poleInfo=new PoleInfo();
        poleInfo.setIsstart(isstart);
        poleInfo.setLastupdatetime(LocalDateTime.now());
        poleInfo.setId(id);
        poleInfo.setLineid(lineid);
        poleInfo.setLinecode(byId.getLinecode());
        poleInfo.setLinename(byId.getLinename());
        if (updateById(poleInfo)) {
            return true;
        }else {
            return false;
        }
    }
}
